package com.swpu.eduservice.controller;


import com.swpu.commonutils.R;
import com.swpu.eduservice.entity.Chapter.ChapterVo;
import com.swpu.eduservice.entity.EduChapter;
import com.swpu.eduservice.service.EduChapterService;
import com.swpu.eduservice.service.EduVideoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author ygq
 * @since 2021-10-16
 */
@RestController
@CrossOrigin
@RequestMapping("/eduservice/chapter")
public class EduChapterController {

    @Autowired
    private EduChapterService eduChapterService;

    @Autowired
    private EduVideoService eduVideoService;

    //课程大纲列表
    @GetMapping("/getChapterVideo/{courseId}")
    public R getChapterVideo(@PathVariable String courseId){
       List<ChapterVo> list =  eduChapterService.getChapterVideoByCOurseId(courseId);
       return R.ok().data("allChapter",list);

    }

    //添加章节
    @PostMapping("/addChapter")
    public R AddChapter(@RequestBody EduChapter eduChapter){
        eduChapterService.save(eduChapter);
        return R.ok();
    }

    //根据章节id查询
    @GetMapping("/getChapterInfo/{chapterId}")
    public R getChapterInfo(@PathVariable String chapterId){
        EduChapter eduChapter  = eduChapterService.getById(chapterId);
        return R.ok().data("chapter",eduChapter);
    }

    //修改章节
    @PostMapping("/updateChapter")
    public R updateChapter(@RequestBody EduChapter eduChapter){
        eduChapterService.updateById(eduChapter);
        return R.ok();
    }

    //删除章节
    @DeleteMapping("{chapterId}")
    public R deleteChapter(@PathVariable String chapterId){

        boolean flag = eduChapterService.deleteChapter(chapterId);
        if(flag) return R.ok();
        else
        return R.error();
    }


}

